package com.nari.ies.ne.nw.server.service.plantDataScore.impl;

import com.nari.ies.ne.nw.server.entity.PvFileScoreEntity;
import com.nari.ies.ne.nw.server.entity.WindDataScoreEntity;
import com.nari.ies.ne.nw.server.entity.WindFileScoreEntity;
import com.nari.ies.ne.nw.server.mapper.FileScoreMapper;
import com.nari.ies.ne.nw.server.mapper.PlantDataMapper;
import com.nari.ies.ne.nw.server.service.plantDataScore.IWindPlantDataScoreService;

import com.nari.ies.ne.nw.server.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class windPlantDataScoreServiceImpl implements IWindPlantDataScoreService {

    @Autowired
    private FileScoreMapper fileScoreMapper;


    @Override
    public WindDataScoreEntity getFDJZDataScore(WindDataScoreEntity windDataScoreEntity) {
        String plantId = windDataScoreEntity.getPlantId();
        //今天00:00
        Date endDate = windDataScoreEntity.getDateTime();
        Date startDate = DateUtil.getYesterdayTime(endDate);
        String fileType = "FDJZ";
        String plantType = "FD";
        String tableName = "HISDB.NW_NE_FILE_DATACHECK_FD_FDJZ";

        //获取昨天该类型文件文件所有数据评分项
        List<Map<String,Object>> fileData = fileScoreMapper.selectFileData(plantId,fileType,plantType,startDate,endDate,tableName);
        //获取该类型文件的数据项和对应分数
        List<Map<String,Object>> dataList = fileScoreMapper.selectFileDataType(fileType,plantType);

        int dataNum =0;
        if(fileData==null||fileData.size()==0||dataList==null){
            windDataScoreEntity.setFDJZ_SPEED_SCORES(0);
            windDataScoreEntity.setFDJZ_DIRECTION_SCORES(0);
            windDataScoreEntity.setFDJZ_TEMPERATURE_SCORES(0);
            windDataScoreEntity.setFDJZ_ACTIVE_SCORES(0);
            windDataScoreEntity.setFDJZ_REACTIVE_SCORES(0);
            windDataScoreEntity.setFDJZ_FACTOR_SCORES(0);
            windDataScoreEntity.setFDJZ_POWER_SCORES(0);
            windDataScoreEntity.setFDJZ_STATUS_SCORES(0);
            windDataScoreEntity.setFDJZ_WIRE_SCORES(0);
        }else {
            dataNum = Integer.parseInt(fileData.get(0).get("NUM").toString());
            for (Map<String, Object> map : dataList) {
                //无数据数量
                int noDataNum = 0;
                //合格数据数量
                int qualifiedNum = 0;
                //不合格数据数量
                int unqualifiedNum = 0;
                String dataType = map.get("DATA_TYPE").toString();
                float score = Float.parseFloat(map.get("SCORE").toString());

                for (Map<String, Object> fileDaMap : fileData) {
                    String fileDataType = fileDaMap.get("DATA_TYPE").toString();
                    if (fileDataType.equals(dataType)) {
                        noDataNum = Integer.parseInt(fileDaMap.get("NO_DATA").toString());
                        qualifiedNum = Integer.parseInt(fileDaMap.get("QUALIFIED_DATA").toString());
                        unqualifiedNum = Integer.parseInt(fileDaMap.get("UNQUALIFIED_DATA").toString());
                    }

                }
                //单项数据得分
                float dataScore = (float) ((qualifiedNum + unqualifiedNum * 0.5 + noDataNum * 0) * score) /  dataNum;

                if(dataType.equals("windSpeed")){
                    windDataScoreEntity.setFDJZ_SPEED_SCORES(dataScore);
                }

                if(dataType.equals("windDirection")){
                    windDataScoreEntity.setFDJZ_DIRECTION_SCORES(dataScore);
                }
                if(dataType.equals("temperature")){
                    windDataScoreEntity.setFDJZ_TEMPERATURE_SCORES(dataScore);
                }
                if(dataType.equals("activePower")){
                    windDataScoreEntity.setFDJZ_ACTIVE_SCORES(dataScore);
                }
                if(dataType.equals("reactivePower")){
                    windDataScoreEntity.setFDJZ_REACTIVE_SCORES(dataScore);
                }
                if(dataType.equals("powerFactor")){
                    windDataScoreEntity.setFDJZ_FACTOR_SCORES(dataScore);
                }
                if(dataType.equals("powerTheory")){
                    windDataScoreEntity.setFDJZ_POWER_SCORES(dataScore);
                }
                if(dataType.equals("fanStatus")){
                    windDataScoreEntity.setFDJZ_STATUS_SCORES(dataScore);
                }
                if(dataType.equals("wire")){
                    windDataScoreEntity.setFDJZ_WIRE_SCORES(dataScore);
                }
            }
        }
        return windDataScoreEntity;
    }

    @Override
    public WindDataScoreEntity getSYZXXDataScore(WindDataScoreEntity windDataScoreEntity) {
        String plantId = windDataScoreEntity.getPlantId();
        //今天00:00
        Date endDate = windDataScoreEntity.getDateTime();
        Date startDate = DateUtil.getYesterdayTime(endDate);
        String fileType = "SYZXX";
        String plantType = "FD";
        String tableName = "HISDB.NW_NE_FILE_DATACHECK_FD_SYZXX";

        //获取昨天该类型文件文件所有数据评分项
        List<Map<String,Object>> fileData = fileScoreMapper.selectFileData(plantId,fileType,plantType,startDate,endDate,tableName);
        //获取该类型文件的数据项和对应分数
        List<Map<String,Object>> dataList = fileScoreMapper.selectFileDataType(fileType,plantType);
        int dataNum =0;
        if(fileData==null||fileData.size()==0||dataList==null){
            windDataScoreEntity.setSYZXX_SHUNT_SCORES(0);
            windDataScoreEntity.setSYZXX_REACTIVE_SCORES(0);
            windDataScoreEntity.setSYZXX_PARALLEL_SCORES(0);
            windDataScoreEntity.setSYZXX_ACTIVE_SCORES(0);
        }else {
            dataNum = Integer.parseInt(fileData.get(0).get("NUM").toString());
            for (Map<String, Object> map : dataList) {
                //无数据数量
                int noDataNum = 0;
                //合格数据数量
                int qualifiedNum = 0;
                //不合格数据数量
                int unqualifiedNum = 0;
                String dataType = map.get("DATA_TYPE").toString();
                float score = Float.parseFloat(map.get("SCORE").toString());

                for (Map<String, Object> fileDaMap : fileData) {
                    String fileDataType = fileDaMap.get("DATA_TYPE").toString();
                    if (fileDataType.equals(dataType)) {
                        noDataNum += Integer.parseInt(fileDaMap.get("NO_DATA").toString());
                        qualifiedNum += Integer.parseInt(fileDaMap.get("QUALIFIED_DATA").toString());
                        unqualifiedNum += Integer.parseInt(fileDaMap.get("UNQUALIFIED_DATA").toString());
                    }
                }
                //单项数据得分
                float dataScore = (float) ((qualifiedNum + unqualifiedNum * 0.5 + noDataNum * 0) * score) /  dataNum;

                if(dataType.equals("activePower")){
                    windDataScoreEntity.setSYZXX_SHUNT_SCORES(dataScore);
                }

                if(dataType.equals("reactivePower")){
                    windDataScoreEntity.setSYZXX_REACTIVE_SCORES(dataScore);
                }
                if(dataType.equals("powerFactor")){
                    windDataScoreEntity.setSYZXX_PARALLEL_SCORES(dataScore);
                }
                if(dataType.equals("wireActivePower")){
                    windDataScoreEntity.setSYZXX_ACTIVE_SCORES(dataScore);
                }
            }
        }
        return windDataScoreEntity;
    }

    @Override
    public WindDataScoreEntity getZTXXDataScore(WindDataScoreEntity windDataScoreEntity) {
        String plantId = windDataScoreEntity.getPlantId();
        //今天00:00
        Date endDate = windDataScoreEntity.getDateTime();
        Date startDate = DateUtil.getYesterdayTime(endDate);
        String fileType = "ZTXX-QXHJ";
        String plantType = "FD";
        String tableName = "HISDB.NW_NE_FILE_DATACHECK_FD_QXHJ";

        //获取昨天该类型文件文件所有数据评分项
        List<Map<String,Object>> fileData = fileScoreMapper.selectFileData(plantId,fileType,plantType,startDate,endDate,tableName);
        //获取该类型文件的数据项和对应分数
        List<Map<String,Object>> dataList = fileScoreMapper.selectFileDataType(fileType,plantType);

        int dataNum =0;

        if(fileData==null||fileData.size()==0||dataList==null){
            windDataScoreEntity.setZTXX_LONGITUDE_SCORES(0);
            windDataScoreEntity.setZTXX_LATITUDE_SCORES(0);
            windDataScoreEntity.setZTXX_ACTIVE_SCORES(0);
            windDataScoreEntity.setZTXX_NORMAL_SCORES(0);
            windDataScoreEntity.setZTXX_PLANNED_SCORES(0);
            windDataScoreEntity.setZTXX_MAINTENANCE_SCORES(0);
            windDataScoreEntity.setZTXX_LIMITED_SCORES(0);
            windDataScoreEntity.setZTXX_LIMITEDNUM_SCORES(0);
            windDataScoreEntity.setZTXX_STANDBY_SCORES(0);
            windDataScoreEntity.setZTXX_STANDBYNUM_SCORES(0);
            windDataScoreEntity.setZTXX_COMMUNICATION_SCORES(0);
            windDataScoreEntity.setZTXX_COMMUNICATIONNUM_SCORES(0);
            windDataScoreEntity.setZTXX_UNPLANNED_SCORES(0);
            windDataScoreEntity.setZTXX_UNPLANNEDNUM_SCORES(0);
            windDataScoreEntity.setZTXX_AMBIENT_SCORES(0);
            windDataScoreEntity.setZTXX_RELATIVE_SCORES(0);
            windDataScoreEntity.setZTXX_PRESSURE_SCORES(0);
            windDataScoreEntity.setZTXX_SPEED10M_SCORES(0);
            windDataScoreEntity.setZTXX_DIRECTION10M_SCORES(0);
            windDataScoreEntity.setZTXX_SPEED30M_SCORES(0);
            windDataScoreEntity.setZTXX_DIRECTION30M_SCORES(0);
            windDataScoreEntity.setZTXX_SPEED50M_SCORES(0);
            windDataScoreEntity.setZTXX_DIRECTION50M_SCORES(0);
            windDataScoreEntity.setZTXX_SPEED70M_SCORES(0);
            windDataScoreEntity.setZTXX_DIRECTION70M_SCORES(0);
            windDataScoreEntity.setZTXX_SPEEDFAN_SCORES(0);
            windDataScoreEntity.setZTXX_DIRECTIONFAN_SCORES(0);
        }else {
            dataNum = Integer.parseInt(fileData.get(0).get("NUM").toString());
            for (Map<String, Object> map : dataList) {
                //无数据数量
                int noDataNum = 0;
                //合格数据数量
                int qualifiedNum = 0;
                //不合格数据数量
                int unqualifiedNum = 0;
                String dataType = map.get("DATA_TYPE").toString();
                float score = Float.parseFloat(map.get("SCORE").toString());

                for (Map<String, Object> fileDaMap : fileData) {
                    String fileDataType = fileDaMap.get("DATA_TYPE").toString();
                    if (fileDataType.equals(dataType)) {
                        noDataNum += Integer.parseInt(fileDaMap.get("NO_DATA").toString());
                        qualifiedNum += Integer.parseInt(fileDaMap.get("QUALIFIED_DATA").toString());
                        unqualifiedNum += Integer.parseInt(fileDaMap.get("UNQUALIFIED_DATA").toString());
                    }
                }
                //单项数据得分
                float dataScore = (float) ((qualifiedNum + unqualifiedNum * 0.5 + noDataNum * 0) * score) / dataNum;

                if(dataType.equals("longitude")){
                    windDataScoreEntity.setZTXX_LONGITUDE_SCORES(dataScore);
                }
                if(dataType.equals("latitude")){
                    windDataScoreEntity.setZTXX_LATITUDE_SCORES(dataScore);
                }
                if(dataType.equals("normalCapacity")){
                    windDataScoreEntity.setZTXX_ACTIVE_SCORES(dataScore);
                }
                if(dataType.equals("normalNumber")){
                    windDataScoreEntity.setZTXX_NORMAL_SCORES(dataScore);
                }

                if(dataType.equals("plannedCapacity")){
                    windDataScoreEntity.setZTXX_PLANNED_SCORES(dataScore);
                }
                if(dataType.equals("plannedNumber")){
                    windDataScoreEntity.setZTXX_MAINTENANCE_SCORES(dataScore);
                }
                if(dataType.equals("limitedCapacity")){
                    windDataScoreEntity.setZTXX_LIMITED_SCORES(dataScore);
                }
                if(dataType.equals("limitedNumber")){
                    windDataScoreEntity.setZTXX_LIMITEDNUM_SCORES(dataScore);
                }

                if(dataType.equals("standbyCapacity")){
                    windDataScoreEntity.setZTXX_STANDBY_SCORES(dataScore);
                }
                if(dataType.equals("standbyNumber")){
                    windDataScoreEntity.setZTXX_STANDBYNUM_SCORES(dataScore);
                }
                if(dataType.equals("interruptCapacity")){
                    windDataScoreEntity.setZTXX_COMMUNICATION_SCORES(dataScore);
                }
                if(dataType.equals("interruptNumber")){
                    windDataScoreEntity.setZTXX_COMMUNICATIONNUM_SCORES(dataScore);
                }

                if(dataType.equals("unplannedCapacity")){
                    windDataScoreEntity.setZTXX_UNPLANNED_SCORES(dataScore);
                }
                if(dataType.equals("unplannedNumber")){
                    windDataScoreEntity.setZTXX_UNPLANNEDNUM_SCORES(dataScore);
                }
                if(dataType.equals("temperature10m")){
                    windDataScoreEntity.setZTXX_AMBIENT_SCORES(dataScore);
                }
                if(dataType.equals("humidity10m")){
                    windDataScoreEntity.setZTXX_RELATIVE_SCORES(dataScore);
                }

                if(dataType.equals("pressure10m")){
                    windDataScoreEntity.setZTXX_PRESSURE_SCORES(dataScore);
                }
                if(dataType.equals("windSpeed10m")){
                    windDataScoreEntity.setZTXX_SPEED10M_SCORES(dataScore);
                }
                if(dataType.equals("windDirection10m")){
                    windDataScoreEntity.setZTXX_DIRECTION10M_SCORES(dataScore);
                }
                if(dataType.equals("windSpeed30m")){
                    windDataScoreEntity.setZTXX_SPEED30M_SCORES(dataScore);
                }

                if(dataType.equals("windDirection30m")){
                    windDataScoreEntity.setZTXX_DIRECTION30M_SCORES(dataScore);
                }
                if(dataType.equals("windSpeed50m")){
                    windDataScoreEntity.setZTXX_SPEED50M_SCORES(dataScore);
                }
                if(dataType.equals("windDirection50m")){
                    windDataScoreEntity.setZTXX_DIRECTION50M_SCORES(dataScore);
                }
                if(dataType.equals("windSpeed70m")){
                    windDataScoreEntity.setZTXX_SPEED70M_SCORES(dataScore);
                }

                if(dataType.equals("windDirection70m")){
                    windDataScoreEntity.setZTXX_DIRECTION70M_SCORES(dataScore);
                }
                if(dataType.equals("hubSpeed")){
                    windDataScoreEntity.setZTXX_SPEEDFAN_SCORES(dataScore);
                }
                if(dataType.equals("hubDirection")){
                    windDataScoreEntity.setZTXX_DIRECTIONFAN_SCORES(dataScore);
                }
            }
        }
        return windDataScoreEntity;
    }

    @Override
    public WindDataScoreEntity getDQYCDataScore(WindDataScoreEntity windDataScoreEntity) {
        String plantId = windDataScoreEntity.getPlantId();
        //今天00:00
        Date endDate = windDataScoreEntity.getDateTime();
        Date startDate = DateUtil.getYesterdayTime(endDate);
        String fileType = "DQYC";
        String plantType = "FD";
        String tableName = "HISDB.NW_NE_FILE_DATACHECK_FD_DQYC";

        //获取昨天该类型文件文件所有数据评分项
        List<Map<String,Object>> fileData = fileScoreMapper.selectFileData(plantId,fileType,plantType,startDate,endDate,tableName);
        //获取该类型文件的数据项和对应分数
        List<Map<String,Object>> dataList = fileScoreMapper.selectFileDataType(fileType,plantType);

        int dataNum =0;
        if(fileData==null||fileData.size()==0||dataList==null){
            windDataScoreEntity.setDQYC_PREDICTION_SCORES(0);
            windDataScoreEntity.setDQYC_PLANNED_SCORES(0);
            windDataScoreEntity.setDQYC_OPERATING_SCORES(0);
        }else {
            dataNum = Integer.parseInt(fileData.get(0).get("NUM").toString());
            for (Map<String, Object> map : dataList) {
                //无数据数量
                int noDataNum = 0;
                //合格数据数量
                int qualifiedNum = 0;
                //不合格数据数量
                int unqualifiedNum = 0;
                String dataType = map.get("DATA_TYPE").toString();
                float score = Float.parseFloat(map.get("SCORE").toString());

                for (Map<String, Object> fileDaMap : fileData) {
                    String fileDataType = fileDaMap.get("DATA_TYPE").toString();
                    if (fileDataType.equals(dataType)) {
                        noDataNum += Integer.parseInt(fileDaMap.get("NO_DATA").toString());
                        qualifiedNum += Integer.parseInt(fileDaMap.get("QUALIFIED_DATA").toString());
                        unqualifiedNum += Integer.parseInt(fileDaMap.get("UNQUALIFIED_DATA").toString());
                    }
                }
                //单项数据得分
                float dataScore = (float) ((qualifiedNum + unqualifiedNum * 0.5 + noDataNum * 0) * score) /dataNum;

                if(dataType.equals("power")){
                    windDataScoreEntity.setDQYC_PREDICTION_SCORES(dataScore);
                }
                if(dataType.equals("capacity")){
                    windDataScoreEntity.setDQYC_PLANNED_SCORES(dataScore);
                }
                if(dataType.equals("operating")){
                    windDataScoreEntity.setDQYC_OPERATING_SCORES(dataScore);
                }
            }
        }
        return windDataScoreEntity;
    }

    @Override
    public WindDataScoreEntity getCDQYCDataScore(WindDataScoreEntity windDataScoreEntity) {
        String plantId = windDataScoreEntity.getPlantId();
        //今天00:00
        Date endDate = windDataScoreEntity.getDateTime();
        Date startDate = DateUtil.getYesterdayTime(endDate);
        String fileType = "CDQYC";
        String plantType = "FD";
        String tableName = "HISDB.NW_NE_FILE_DATACHECK_FD_CDQYC";

        //获取昨天该类型文件文件所有数据评分项
        List<Map<String,Object>> fileData = fileScoreMapper.selectFileData(plantId,fileType,plantType,startDate,endDate,tableName);
        //获取该类型文件的数据项和对应分数
        List<Map<String,Object>> dataList = fileScoreMapper.selectFileDataType(fileType,plantType);

        int dataNum =0;
        if(fileData==null||fileData.size()==0||dataList==null){
            windDataScoreEntity.setCDQYC_PREDICTION_SCORES(0);
            windDataScoreEntity.setCDQYC_OPERATING_SCORES(0);
        }else {
            dataNum = Integer.parseInt(fileData.get(0).get("NUM").toString());
            for (Map<String, Object> map : dataList) {
                //无数据数量
                int noDataNum = 0;
                //合格数据数量
                int qualifiedNum = 0;
                //不合格数据数量
                int unqualifiedNum = 0;
                String dataType = map.get("DATA_TYPE").toString();
                float score = Float.parseFloat(map.get("SCORE").toString());

                for (Map<String, Object> fileDaMap : fileData) {
                    String fileDataType = fileDaMap.get("DATA_TYPE").toString();
                    if (fileDataType.equals(dataType)) {
                        noDataNum += Integer.parseInt(fileDaMap.get("NO_DATA").toString());
                        qualifiedNum += Integer.parseInt(fileDaMap.get("QUALIFIED_DATA").toString());
                        unqualifiedNum += Integer.parseInt(fileDaMap.get("UNQUALIFIED_DATA").toString());
                    }
                }
                //单项数据得分
                float dataScore = (float) ((qualifiedNum + unqualifiedNum * 0.5 + noDataNum * 0) * score) / dataNum;

                if(dataType.equals("power")){
                    windDataScoreEntity.setCDQYC_PREDICTION_SCORES(dataScore);
                }
                if(dataType.equals("capacity")){
                    windDataScoreEntity.setCDQYC_OPERATING_SCORES(dataScore);
                }
            }
        }
        return windDataScoreEntity;
    }

    @Override
    public WindDataScoreEntity getTJXXDataScore(WindDataScoreEntity windDataScoreEntity) {
        String plantId = windDataScoreEntity.getPlantId();
        //今天00:00
        Date endDate = windDataScoreEntity.getDateTime();
        Date startDate = DateUtil.getYesterdayTime(endDate);
        String fileType = "TJXX";
        String plantType = "FD";
        String tableName = "HISDB.NW_NE_FILE_DATACHECK_FD_TJXX";

        //获取昨天该类型文件文件所有数据评分项
        List<Map<String,Object>> fileData = fileScoreMapper.selectFileData(plantId,fileType,plantType,startDate,endDate,tableName);
        //获取该类型文件的数据项和对应分数
        List<Map<String,Object>> dataList = fileScoreMapper.selectFileDataType(fileType,plantType);

        int dataNum =0;
        if(fileData==null||fileData.size()==0||dataList==null){
            windDataScoreEntity.setTJXX_INSTALLED_SCORES(0);
            windDataScoreEntity.setTJXX_FANS_SCORES(0);
            windDataScoreEntity.setTJXX_ACTIVE_SCORES(0);
            windDataScoreEntity.setTJXX_REACTIVE_SCORES(0);
            windDataScoreEntity.setTJXX_AVAILABLE_SCORES(0);
        }else {
            dataNum = Integer.parseInt(fileData.get(0).get("NUM").toString());
            for (Map<String, Object> map : dataList) {
                //无数据数量
                int noDataNum = 0;
                //合格数据数量
                int qualifiedNum = 0;
                //不合格数据数量
                int unqualifiedNum = 0;
                String dataType = map.get("DATA_TYPE").toString();
                float score = Float.parseFloat(map.get("SCORE").toString());

                for (Map<String, Object> fileDaMap : fileData) {
                    String fileDataType = fileDaMap.get("DATA_TYPE").toString();
                    if (fileDataType.equals(dataType)) {
                        noDataNum += Integer.parseInt(fileDaMap.get("NO_DATA").toString());
                        qualifiedNum += Integer.parseInt(fileDaMap.get("QUALIFIED_DATA").toString());
                        unqualifiedNum += Integer.parseInt(fileDaMap.get("UNQUALIFIED_DATA").toString());
                    }
                }
                //单项数据得分
                float dataScore = (float) ((qualifiedNum + unqualifiedNum * 0.5 + noDataNum * 0) * score) /dataNum;

                if(dataType.equals("capacity")){
                    windDataScoreEntity.setTJXX_INSTALLED_SCORES(dataScore);
                }

                if(dataType.equals("fansNumber")){
                    windDataScoreEntity.setTJXX_FANS_SCORES(dataScore);
                }
                if(dataType.equals("activePower")){
                    windDataScoreEntity.setTJXX_ACTIVE_SCORES(dataScore);
                }
                if(dataType.equals("reactivePower")){
                    windDataScoreEntity.setTJXX_REACTIVE_SCORES(dataScore);
                }
                if(dataType.equals("availablePower")){
                    windDataScoreEntity.setTJXX_AVAILABLE_SCORES(dataScore);
                }
            }
        }
        return windDataScoreEntity;
    }

}
